package gofish.servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import gofish.dao.GoFishDAOHandler;
import gofish.json.JsonArray;
import gofish.json.JsonObject;

/**
 * 
 *
 * @author Matt Lauber
 */
public class GetUsers extends HttpServlet {

	/**
	 * doPost() 
	 */
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("application/json");

		PrintWriter out = response.getWriter();
		try {
		JsonObject jsonResponse = new JsonObject();
		
		//int gid = Integer.parseInt(request.getParameter("gid"));
		
		
		GoFishDAOHandler handler = new GoFishDAOHandler(getServletContext());
		
		String[] names = handler.getLoggedInUsers();
		
		JsonArray users = new JsonArray();
		
		if (names != null) { 
		
			for (String s : names) {
				users.add(s);
			}
			jsonResponse.add("users", users);
		
		}
		else {
			//jsonResponse.add("users", new JsonArray());
			
		}
		
		
		
		//return eid_diff;
		
		out.print(jsonResponse);
		} catch (Exception e) {
			JsonObject json = new JsonObject();
			StringBuilder builder = new StringBuilder();
			for (StackTraceElement elem : e.getStackTrace()) {
				builder.append(elem.toString() + " ");
			}
			json.add("error", builder.toString());
			out.print(json.toString());
		}
		out.flush();
	}
}
